package com.qifan.energy.widget.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.qifan.energy.mvp.callback.IEventCallback;
import com.qifan.energy.R;

public class UpdateNoticeDialog extends DialogFragment {

    private TextView contentTv;

    private IEventCallback callback = new IEventCallback() {

        @Override
        public void onCancel() {

        }

        @Override
        public void onComfirm() {

        }
    };

    /* 显示 */
    public UpdateNoticeDialog showNow2(@Nullable FragmentManager manager, @Nullable String tag) {
        showNow(manager, tag);
        return this;
    }

    /* 对外提供 */
    public UpdateNoticeDialog setEventCallback(IEventCallback callback) {
        this.callback = callback;
        return this;
    }

    /* 对外提供 */
    public UpdateNoticeDialog setContent(String content){
        if (contentTv != null && !TextUtils.isEmpty(content)) {
            contentTv.setText(content);
        }
        return this;
    }

    /**
     * 设置主题需要在 onCreate() 方法中调用 setStyle() 方法
     */
    /*@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        *//*setStyle(DialogFragment.STYLE_NO_TITLE, R.style.CustomDialog);*//*
        setStyle(DialogFragment.STYLE_NO_FRAME,R.style.NormalDialogStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        View view = inflater.inflate(R.layout.dialog_update_notice, container);
        findView(view);
        return view;
    }*/

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // 设置主题的构造方法
        // AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.CustomDialog);
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_update_notice, null);
        builder.setView(view);
        findView(view);
        return builder.create();
    }

    /* 设置View */
    private void findView(View view) {
        contentTv = view.findViewById(R.id.content_tv);
        TextView confirmTv = view.findViewById(R.id.confirm_tv);
        confirmTv.setOnClickListener(v -> {
            dismiss();
            if (callback != null) callback.onComfirm();
        });
    }
}
